package com.server.base.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;

import com.server.base.ConstantCommon;
import com.server.base.SystemConfig;
import com.server.model.LogUserLike;

/**
 * Created by xiehuilin on 2015/8/18.
 * 队列工具类
 */
public class MQUtil {

    private static final Log logger = LogFactory.getLog(MQUtil.class);

    private static JmsTemplate jmsTemplate;

    static{
        jmsTemplate = (JmsTemplate)ToolSpring.getBean("jmsTemplate");
    }


    /**
     * 新增同城活动队列
     * @param infoType
     * @param infoId
     * @param source
     */
    public static void insertQQActivityQueue(final String infoType, final long infoId, final String source){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_TYPE", infoType);
            param.put("INFO_ID", infoId);
            param.put("MSG_SOURCE", source);

            jmsTemplate.send(ConstantCommon.INSERT_QQ_ACTIVITY_QUEUE, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息insertQQActivityQueue异常-" + infoType + "-" + infoId + "-" + source);

        }



    }

    /**
     * 编辑同城活动队列
     * @param infoType
     * @param infoId
     * @param source
     */
    public static void updateQQActivityQueue(final String infoType, final long infoId, final String source){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_TYPE", infoType);
            param.put("INFO_ID", infoId);
            param.put("MSG_SOURCE", source);

            jmsTemplate.send(ConstantCommon.UPDATE_QQ_ACTIVITY_QUEUQ, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息updateQQActivityQueue异常-" + infoType + "-" + infoId + "-" + source);

        }



    }

    /**
     * 删除同城活动
     * @param infoType
     * @param infoId
     * @param source
     * @param operator user/manager/system表示“用户删除/运维删除/系统自动删除
     */
    public static void deleteQQActivityQueue(final String infoType, final long infoId, final String source, final String operator){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_TYPE", infoType);
            param.put("INFO_ID", infoId);
            param.put("MSG_SOURCE", source);
            param.put("OPERATOR", operator);

            jmsTemplate.send(ConstantCommon.DELETE_QQ_ACTIVITY_QUEUQ, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息deleteQQActivityQueue异常-" + infoType + "-" + infoId + "-" + source + "-" + operator);

        }



    }

    /**
     * 在mq队列中新增一条需要更新活动点击数的记录
     * @param infoId
     * @param infoType
     */
    public static void insertInfoHitsQueue(final long infoId, final String infoType){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_TYPE", infoType);
            param.put("INFO_ID", infoId);

            jmsTemplate.send(ConstantCommon.HITS_INFO_QUEUQ, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息insertInfoHitsQueue异常-" + infoType + "-" + infoId);

        }

    }

    /**
     * 在mq队列中新增一条需要更新活动分享数的记录
     * @param infoId
     * @param infoType
     */
    public static void insertInfoShareQueue(final long infoId, final String infoType){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_TYPE", infoType);
            param.put("INFO_ID", infoId);

            jmsTemplate.send(ConstantCommon.SHARE_INFO_QUEUQ, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息insertInfoShareQueue异常-" + infoType + "-" + infoId);

        }




    }

    /**
     * 记录用户投票的队列 到mq中写入数据库
     * @param voteId
     * @param userId
     * @param ip
     * @param agent
     * @param options
     */
    public static void insertJoinVoteQueue(final long voteId, final long userId, final String ip, final String agent, final List<String> options){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("VOTE_ID", voteId);
            param.put("USER_ID", userId);
            param.put("IP", ip);
            param.put("AGENT", agent);
            param.put("OPTION", options);

            jmsTemplate.send(ConstantCommon.INFO_JOIN_VOTE_QUEUQ, new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();
            logger.error("插入mq消息insertJoinVoteQueue异常-" + voteId + "-" + userId + "-" + ip + "-" + agent + "-" + options);

        }



    }

    /**
     * 插入店铺点击数mq
     * @param shopId
     */
    public static void insertShopHitsQueue(final long shopId){

        try{

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("SHOP_ID", shopId);

            jmsTemplate.send(ConstantCommon.HITS_SHOP_QUEUE,  new MQTextMessageCreater(param));

        }catch(Exception e){

            e.printStackTrace();

        }

    }
    
    
    /**
     * 用户报名发送短信提示
     * @param infoId
     * @param userId
     * wu
     */
    public static void joinPartyQueue(final long infoId,final long userId,String phoneNum){
        try{
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_ID", infoId);
            param.put("USER_ID", userId);
            param.put("PHONE_NUM", phoneNum);
            if(SystemConfig.getJoinPartySendMsgMQStatus()){
            	jmsTemplate.send(ConstantCommon.INFO_USER_JOINPARTY_QUEUQ, new MQTextMessageCreater(param));
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("报名加入MQ异常" + infoId + "-" + userId);
        }
    }
    
    /***
     * 用户点赞
     * @param logUserLike
     */
    public static void userLikeQueue(final LogUserLike logUserLike){
        try{
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_ID", logUserLike.getInfoId());
            param.put("INFO_TYPE", logUserLike.getInfoType());
            param.put("USER_ID", logUserLike.getUserId());
            if(SystemConfig.getJoinPartySendMsgMQStatus()){
            	jmsTemplate.send(ConstantCommon.INFO_USER_LIKE_QUEUQ, new MQTextMessageCreater(param));
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("用户点赞加入MQ异常");
        }
    }
    
    /***
     * 用户评论
     * @param infoId
     * @param infoType
     * @param userId
     */
    public static void userCommentQueue(final long infoId,final String infoType,final long userId){
        try{
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("INFO_ID", infoId);
            param.put("INFO_TYPE", infoType);
            param.put("USER_ID", userId);
            if(SystemConfig.getJoinPartySendMsgMQStatus()){
            	jmsTemplate.send(ConstantCommon.INFO_USER_COMMENT_QUEUQ, new MQTextMessageCreater(param));
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("用户评论加入MQ异常");
        }
    }


}
